package com.nnfe.fyd.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.nnfe.fyd.AppConfig;

/**
 * ----------------------------------
 * Created by xyd on 2017/3/15.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */
public class TabBar extends LinearLayout implements View.OnClickListener {

    private View currenTab;

    private View[] tabs;
    private int childCount;

    private TabListener tabListener;

    public TabBar(Context context) {
        super(context);
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        childCount = getChildCount();
        tabs = new View[childCount];
        for (int i = 0; i < childCount; i++) {
            tabs[i] = getChildAt(i);
            tabs[i].setOnClickListener(this);
            tabs[i].setTag(AppConfig.FRAGMENT_TAB + i);
        }
    }

    public String getTag() {
        if (currenTab != null) {
            return currenTab.getTag().toString();
        }
        return null;
    }

    /**
     * 显示简易未读通知  ---
     *
     * @param index
     */
    private void showSimpleUnreadIndicate(int index) {
        FrameLayout layout = (FrameLayout) tabs[index];
        View view = layout.getChildAt(1);
        view.setVisibility(VISIBLE);
    }

    /**
     * 隐藏简易未读通知  ---
     *
     * @param index
     */
    private void hideSimpleUnreadIndicate(int index) {
        FrameLayout layout = (FrameLayout) tabs[index];
        View view = layout.getChildAt(1);
        view.setVisibility(GONE);
    }

    /**
     * 显示某一个tab
     *
     * @param index
     */
    public void changeTab(int index) {
        if (index < 0)
            throw new RuntimeException("tab index cannot be negative");
        if (tabs != null && childCount > 0 && childCount > index) {
            setCurrenTabAndFragment(tabs[index]);
        }

    }

    /**
     * 这里要求 fragment 和 tab 设置的 tag 相同
     *
     * @param tabview
     */
    private void setCurrenTabAndFragment(View tabview) {
        if (tabview == null) {
            return;
        }
        if (tabListener == null) {
            return;
        }

        // 重复点击同一个 view 则调用刷新
        if (currenTab != null && tabview.getTag().equals(currenTab.getTag())) {
            tabListener.refreshView(currenTab.getTag().toString());
            return;
        }

        // 将之前的tab置灰 并且 隐藏该 fragment
        if (currenTab != null) {
            currenTab.setSelected(false);
            tabListener.hideFragment(currenTab.getTag().toString());
        }

        // 刷新当前view引用 显示当前fragment
        currenTab = tabview;
        currenTab.setSelected(true);
        tabListener.showFragment(tabview.getTag().toString());
    }

    @Override
    public void onClick(View view) {
        setCurrenTabAndFragment(view);
    }

    public void setTabListener(TabListener listener) {
        this.tabListener = listener;
    }

    public interface TabListener {

        /**
         * 显示新tab页
         *
         * @param fragmentName
         */
        void showFragment(String fragmentName);

        /**
         * 隐藏新tab页
         *
         * @param fragmentName
         */
        void hideFragment(String fragmentName);

        /**
         * 刷新当前tab页
         */
        void refreshView(String fragmentName);

    }
    // tabbar 作用
    // 1. 点击跳转到对应的tab
    // 2. 点击已经显示的tab 刷新


}
